package com.bmob.lover.ui.fragment.lover

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.config.SelectMimeType
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.theone.common.ext.afterTextChange
import com.bmob.lover.data.model.bean.Lover
import com.bmob.lover.data.model.bean.TheFile
import com.bmob.lover.databinding.FragmentLoverPublishBinding
import com.bmob.lover.ui.fragment.base.BaseFileUploadFragment
import com.bmob.lover.viewmodel.base.BaseBmobActionViewModel
import com.bmob.lover.viewmodel.LoverPublishViewModel
import com.bmob.lover.viewmodel.request.AlbumSaveViewModel
import com.bmob.lover.viewmodel.request.LoverSaveViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-06 14:40
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class LoverPublishFragment :
    BaseFileUploadFragment<Lover, LoverPublishViewModel, FragmentLoverPublishBinding>() {

    private val mPublishVm: LoverSaveViewModel by viewModels()
    private val mAlbumVm: AlbumSaveViewModel by viewModels()

    override fun getPublishViewModel(): BaseBmobActionViewModel<Lover> = mPublishVm

    override fun onPictureNoneSelectClick() {
        mPublishVm.save(getViewModel().content.get())
    }

    override fun getTitle(): String = "发布"

    override fun onUploadFileSuccess(files: List<TheFile>) {
        // 视频发到动态，图片发到相册
        if(files[0].type == SelectMimeType.TYPE_VIDEO){
            mPublishVm.save(getViewModel().content.get(), files)
        }else{
            mAlbumVm.save(getViewModel().content.get(),files)
        }
    }

    override fun createObserver() {
        addLoadingObserve(mAlbumVm)
        super.createObserver()
        mAlbumVm.addObserve()
    }

    override fun initView(root: View) {
        super.initView(root)
        getDataBinding().etContent.afterTextChange {
            mSubmit?.isEnabled = it.isNotEmpty()
        }
    }

    override fun onAddPictureClick() {
        QMUIBottomSheet.BottomListSheetBuilder(mActivity).apply {
            setAddCancelBtn(true)
            setGravityCenter(true)
            addItem("图片")
            addItem("视频")
            setOnSheetItemClickListener { dialog, itemView, position, tag ->
                dialog.dismiss()
                val max =  when(tag){
                    "图片"-> {
                        selectImages()
                        getMaxSelectNum()
                    }
                    else -> {
                        selectVideo()
                        1
                    }
                }
                setMaxSelectNum(max)
            }
        }.build().show()

    }

    override fun getRecyclerView(): RecyclerView = getDataBinding().recyclerView

}